package cn.ezios.huo.s3.core;

import cn.ezios.huo.s3.bean.HuoStorageAccessCard;
import lombok.AllArgsConstructor;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.S3Configuration;
import software.amazon.awssdk.services.s3.presigner.S3Presigner;
import software.amazon.awssdk.services.s3.presigner.model.GetObjectPresignRequest;

import java.net.URL;
import java.time.Duration;

/**
 * 实现类
 */
public class HuoS3StorageCore implements HuoStorageCore {

    /**
     * 信息卡
     */
    private final HuoStorageAccessCard accessCard;

    private S3Client client;
    private S3Presigner preSigner;

    /**
     * 初始化构造器
     * @param accessCard
     */
    public HuoS3StorageCore(HuoStorageAccessCard accessCard) {
        this.accessCard = accessCard;
        createClientAndPreSigner();
    }

    /**
     * 初始化S3客户端和预签名器
     */
    private void createClientAndPreSigner() {
        this.client = S3Client.builder().region(Region.of(accessCard.getRegion()))
                .credentialsProvider(
                        StaticCredentialsProvider.create(AwsBasicCredentials.create(accessCard.getAccessKey(), accessCard.getSecretKey()))
                )
                .endpointOverride(java.net.URI.create(accessCard.getEndpoint()))
                .build();
        this.preSigner = S3Presigner.builder().region(Region.of(accessCard.getRegion()))
                .credentialsProvider(
                        this.client.serviceClientConfiguration().credentialsProvider()
                )
                .endpointOverride(java.net.URI.create(accessCard.getEndpoint()))
                .serviceConfiguration(S3Configuration.builder().checksumValidationEnabled(false).pathStyleAccessEnabled(true).build())
                .build();
    }


    /**
     * 获取预览文件的预签名URL
     *
     * @param key
     */
    @Override
    public String getPreviewUrl(String key) {
        URL url = preSigner
                .presignGetObject(GetObjectPresignRequest.builder().signatureDuration(accessCard.getExpireMinutes()).getObjectRequest(getObjReq -> {
                    getObjReq.bucket(accessCard.getBucketName()).key(key);
                }).build()).url();
        return url.toString();
    }

    /**
     * 获取上传文件的预签名URL
     *
     * @param key
     */
    @Override
    public String getUploadUrl(String key) {
        URL url = preSigner.presignPutObject(req -> {
            req.signatureDuration(Duration.ofMinutes(30)).putObjectRequest(putObjReq -> {
                putObjReq.bucket(accessCard.getBucketName()).key(key).build();
            });
        }).url();
        return url.toString();
    }

    /**
     * 删除文件
     *
     * @param key
     */
    @Override
    public void delete(String key) {
        client.deleteObject(req->{
            req.bucket(accessCard.getBucketName()).key(key).build();
        });
    }

    /**
     * 获取存储桶名称
     *
     * @return
     */
    @Override
    public String bucket() {
        return accessCard.getBucketName();
    }
}
